# -*- coding: utf-8 -*-
'''
Created on 17-05-2011

@author: Filon
'''

import re

'''
Creates regexp
'''  
def run(format, min = None, max = None):
    result = ''
    if min == None:
        min = "00:00"
    if max == None:
        max = "23:59"
    if format == '%H:%M':
        if is_valid(format, min, max) == 0:
            result = reg_time0(min, max)
        elif is_valid(format, min, max) == 1:
            result = reg_time1(min, max)
        else:
            result = reg_time2(min, max)
        result = r'^(' + result + ')(\+((0[0-9])|(1[1-2])))?$'
        
    return result


'''
Function responsible for validating format and limits
'''
def is_valid(format, min, max):
    if format == "%H:%M":
        H1 = min[0:2]
        M1 = min[3:5]
        
        H2 = max[0:2]
        M2 = max[3:5]
        
        if len(min) != 5:
            raise Exception('min has wrong format')
        if len(max) != 5:
            raise Exception('max has wrong format') 
        
        
        if H1 > '23' or H1 < '00' or M1 > '59' or M1 < '00':
            raise Exception('min has wrong format') 
       
        if H2 > '23' or H2 < '00' or M2 > '59' or M2 < '00':
            raise Exception('max has wrong format') 
       
       
        
        if H1 > H2:
            raise Exception('min is higher than max')
        elif H1 == H2 and M1 > M2:
            raise Exception('min is higher than max')
        else:
            if (int(H2) - int(H1)) > 1:
                return 2
            elif (int(H2) - int(H1)) == 1:
                return 1
            else:
                return 0
    else:
        raise Exception('unsuported format. Try: %H:%M')




'''
Function responsible for creating regexp for instance when both hours are the same
'''
def reg_time0(min, max):   
    
    H11 = min[0:1]
    M11 = min[3:4] 
    H12 = min[1:2]
    M12 = min[4:5]
    
    H21 = max[0:1]
    M21 = max[3:4] 
    H22 = max[1:2]
    M22 = max[4:5]
    
    if M11 == M21:
        return H11 + H12 + ':' + M11 + '[' + M12 + '-' + M22 + ']'
    if int(M21) - int(M11) == 1:
        return '(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|(' + H11 + H12 + ':' + M21 + '[0-' + M22 + '])'
    if int(M21) - int(M11) > 1:
        return '(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|(' + H11 + H12 + ':' + M21 + '[0-' + M22 + '])|(' + H11 + H12 + ':' + '[' + str(int(M11) + 1) + '-' + str(int(M21) - 1) + ']' + '[0-9])'


'''
Function responsible for creating regexp for instance when there is one hour difference between hours
'''
def reg_time1(min, max):      
    H11 = min[0:1]
    M11 = min[3:4] 
    H12 = min[1:2]
    M12 = min[4:5]
    
    H21 = max[0:1]
    M21 = max[3:4] 
    H22 = max[1:2]
    M22 = max[4:5]
    
    if M11 == M21:
        return r'(' + H11 + H12 + ':[' + M11 + '-5]' + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':[0-' + M21 + ']' + '[0-' + M22 + '])'
    elif int(M11) < 5 and int(M21) > 0:
        return r'(' + H11 + H12 + ':[' + str(int(M11) + 1) + '-5]' + '[0-9])|' + '(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':[0-' + str(int(M21) - 1) + ']' + '[0-9])|' + '(' + H21 + H22 + ':' + M21 + '[0-' + M22 + '])'
    elif int(M11) < 5 and int(M21) == 0:
        return r'(' + H11 + H12 + ':[' + str(int(M11) + 1) + '-5]' + '[0-9])|' + '(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':' + M21 + '[0-' + M22 + '])'
    elif int(M11) == 5 and int(M21) > 0:
        return r'(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':[0-' + str(int(M21) - 1) + ']' + '[0-9])|' + '(' + H21 + H22 + ':' + M21 + '[0-' + M22 + '])'
    else:
        return r'(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':' + M21 + '[0-' + M22 + '])'



'''
Function responsible for creating regexp for instance when there is more than one hour difference between hours
'''
def reg_time2(min, max):  
    H1 = min[0:2]
    M1 = min[3:5]
        
    H2 = max[0:2]
    M2 = max[3:5] 
     
    H11 = min[0:1]
    M11 = min[3:4] 
    H12 = min[1:2]
    M12 = min[4:5]
    
    H21 = max[0:1]
    M21 = max[3:4] 
    H22 = max[1:2]
    M22 = max[4:5]
    
    ret = ''
     
    
    if M11 == M21:
        ret = '(' + H11 + H12 + ':[' + M11 + '-5]' + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':[0-' + M21 + ']' + '[0-' + M22 + '])'
    elif int(M11) < 5 and int(M21) > 0:
        ret = '(' + H11 + H12 + ':[' + str(int(M11) + 1) + '-5]' + '[0-9])|' + '(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':[0-' + str(int(M21) - 1) + ']' + '[0-9])|' + '(' + H21 + H22 + ':' + M21 + '[0-' + M22 + '])' 
    elif int(M11) < 5 and int(M21) == 0:
        ret = '(' + H11 + H12 + ':[' + str(int(M11) + 1) + '-5]' + '[0-9])|' + '(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':' + M21 + '[0-' + M22 + '])' 
    elif int(M11) == 5 and int(M21) > 0:
        ret = '(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':[0-' + str(int(M21) - 1) + ']' + '[0-9])|' + '(' + H21 + H22 + ':' + M21 + '[0-' + M22 + '])' 
    else:
        ret = '(' + H11 + H12 + ':' + M11 + '[' + M12 + '-9])|' + '(' + H21 + H22 + ':' + M21 + '[0-' + M22 + '])'


    if(H11 == H21):
        two = '|(' + H11 + '[' + str(int(H12) + 1) + '-' + str(int(H22) - 1) + ']:[0-5][0-9])'
    elif int(H1) < 9 and int(H2) > 20:
        two = '|(0[' + str(int(H12) + 1) + '-9]:[0-5][0-9])' + '|(1[0-9]:[0-5][0-9])' + '|(2[0-' + str(int(H22) - 1) + ']:[0-5][0-9])'
    elif int(H1) < 9 and int(H2) > 10:    
        two = '|(0[' + str(int(H12) + 1) + '-9]:[0-5][0-9])' + '|(1[0-' + str(int(H22) - 1) + ']:[0-5][0-9])'
    elif int(H1) > 9 and int(H2) > 20:
        two = '|(1[' + str(int(H12) + 1) + '-9]:[0-5][0-9])' + '|(2[0-' + str(int(H22) - 1) + ']:[0-5][0-9])'
   
        
    return r'' + ret + two;




if __name__ == '__main__':
    xxx = run("%H:%M", "11:47", "23:45")
 
    print (xxx)
    
    print(re.match(xxx, "21:59"))
    print(re.match(xxx, "12:29"))
    print(re.match(xxx, "12:29+01"))
    
    
    
